/**
 * @file 轨迹工具函数
 * @description 轨迹数据相关的工具函数
 * @date 2024-08-20
 */

import type { TrackPoint, TrackPointsData } from '@/types/track';

/**
 * 将API返回的轨迹点转换为百度地图轨迹点格式
 * @param points 轨迹点数组
 * @returns 转换后的轨迹点和颜色偏移
 */
export function convertTrackPointsForBMap(points: TrackPoint[]): TrackPointsData {
  if (!points || !points.length) return { trackPoints: [], colorOffset: [] };
  
  const result = [];
  const colorOffset = []; // 渐变色偏移
  
  for (let i = 0; i < points.length; i++) {
    const point = points[i];
    const position = point.position;
    
    if (!position) continue;
    
    // 创建百度地图点对象
    const bPoint = new window.BMapGL.Point(position.lng, position.lat);
    
    // 创建轨迹点对象
    const trackPoint = new window.Track.TrackPoint(bPoint, {
      // 添加时间戳
      time: new Date(point.timestamp).getTime()
    });
    
    result.push(trackPoint);
    
    // 生成渐变色的偏移值
    const offset = i / (points.length - 1);
    colorOffset.push(Math.min(Math.max(offset, 0), 1));
  }
  
  return { trackPoints: result, colorOffset };
} 